<?php
namespace app\admin\model;

use app\admin\model\Channel;
use think\Model;
use think\Db;

/**
 * 签名管理模型
 * @package app\admin\model
 */
class BusPlatTempConfig extends Model
{
    // 设置当前模型对应的完整数据表名称
    // protected $table = 'bus_sign';

    public function searchMarkingAttr($query, $value, $data)
    {
        if(isMmsOrSms()){
            $query->where('marking',1);
        }
        
    }
	/**
     * [getPlatfromAlls 获取通道信息]
     * @author   .guo
     * @DataTime 2019-05-30
     * @param    [type]     $request [description]
     * @return   [type]              [description]
     */
    public static function getPlatTempConfig($request)
    {
        // $data = [];


        $query = self::withSearch(['marking'])
            ->alias('pt')
            ->field('pt.*,bp.platfrom')
            ->join('bus_platfrom bp', 'bp.plat_id = pt.plat_id');

        if($request['plat_id'])
        {
            $query = $query->where('pt.plat_id',$request['plat_id']);
        }
        if($request['temp_id'])
        {
            $query = $query->where('pt.temp_id' , 'like' , '%' . trim($request['temp_id']) . '%' );
        }

        $data = $query->order('temp_id,pt.id')
            ->paginate($request['limit'], false, $request)
            ->each(function($item, $key) {
                switch ($item->operator) {

                    case '10086':
                        $item->operator = '移动';
                        break;
                    case '10000':
                        $item->operator = '电信';
                        break;
                    case '10010':
                        $item->operator = '联通';
                        break;
                    case '90001':
                        $item->operator = '三网';
                        break;
                    default:
                        $item->operator = '未定义';
                        break;
                }


        });
            // ->each(function($item, $key) {
                // switch ($item->plat_type) {

                //     case 'CMPP':
                //         if ($item->onoff == 1) {

                //             $item->plat_type_ = '<span class="layui-badge layui-bg-green onoff-1">开启</span>';
                //         } else {

                //             $item->plat_type_ = '<span class="layui-badge onoff-0">禁用</span>';
                //         }

                //         if ($item->cmpp_status == 1) {

                //             $item->cmpp_status_ = '<span class="layui-badge layui-bg-green cmpp_status-1">通讯中</span>';
                //         } else {

                //             $item->cmpp_status_ = '<span class="layui-badge cmpp_status-1">未连接</span>';
                //         }


                //         $item->cmpp_service = $item->desc ?: '';

                //         break;

                //     default:

                //         $item->plat_type_ = '';
                //         $item->cmpp_service = '';
                //         break;
                // }


        // });

        // $redis = $this->getRides();

        // foreach ($query as $key => $value) {

        //     // $speed = $redis->get("HTTP_SL_".$value['plat_id']);

        //     $data[$key]['speed'] = $speed === false ? "未设置" : $speed."每秒";
        // }

        return $data;
    }

    public static function add($data)
    {
    	try {
            $data['connect_id'] = self::findTempId($data['temp_id']);
    		self::create($data);
    		return ['code'=> '200', 'msg' => '新增成功'];
    	} catch (\Exception $e) {
    		return ['code' => '422', 'msg' => '请检查是否重复添加'];
    	}
    }

    public static function upd($data)
    {
        try {
            $data['connect_id'] = self::findTempId($data['temp_id']);
            self::update($data);
            deleteCache();
            return ['code' => '200', 'msg' => '修改成功'];
        } catch (\Exception $e) {
            return ['code' => '422', 'msg' => '修改失败,请检查是否已存在'];
        }
    }

    public static function findTempId($tempId)
    {
        $tempConfigInfo = self::where('temp_id',$tempId)->find();
        if($tempConfigInfo){
            return $tempConfigInfo->connect_id;
        }else{
            return self::max('connect_id')+1;
        }
    }
}